﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Telerik.Web.UI;

public partial class SiteWorx_EditPage : System.Web.UI.Page
{
    public Asah.SiteWorx.SiteMap.Page MyPage
    {
        get
        {
            if (Session["MyEditPage"] == null)
                if (Request.QueryString["pageId"] != null && Request.QueryString["pageId"].ToString().Length > 0)
                    Session["MyEditPage"] = Asah.SiteWorx.SiteMap.Page.GetPage(int.Parse(Request.QueryString["pageId"]));
            return (Asah.SiteWorx.SiteMap.Page)Session["MyEditPage"];
        }
        set
        {
            Session["MyEditPage"] = value;
        }
    }

    protected void cdsPage_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = MyPage;
    }

    protected void cdsPage_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e)
    {
        Csla.Data.DataMapper.Map(e.Values, MyPage);
        SavePage();
    }

    private void SavePage()
    {
        if (MyPage.IsValid)
        {
            MyPage.Save();
            ScriptManager.RegisterStartupScript(this, Page.GetType(), "GoToUpdatedPage", "GoToUpdatedPage('../" + MyPage.FileName + "');", true);
            MyPage = null;
        }
        else
        {
            CustomValidator validator;
            foreach (Csla.Validation.BrokenRule rule in MyPage.BrokenRulesCollection)
            {
                validator = new CustomValidator();
                validator.ErrorMessage = rule.Description;
                validator.IsValid = false;
                Page.Validators.Add(validator);
            }
        }
    }

    protected void fvEditPage_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName == "Cancel")
        {
            MyPage = null;
            ScriptManager.RegisterStartupScript(this, Page.GetType(), "CloseScript", "CloseOnReload();", true);
        }
    }

    protected void fvEditPage_DataBound(object sender, EventArgs e)
    {
        Literal litPageUpdateSection = (Literal)fvEditPage.FindControl("litPageUpdateSection");
        ProfileCommon profileCommon;

        if (MyPage.CreatedDate.Length > 0)
        {
            litPageUpdateSection.Text += "Created at " + MyPage.CreatedDate;
            if (MyPage.CreatedBy != null)
            {
                profileCommon = Profile.GetProfile(MyPage.CreatedBy.Name);
                if (profileCommon.DisplayName.Length > 0)
                    litPageUpdateSection.Text += " by <a href=\"mailto:" + MyPage.CreatedBy.Email + "\">" + profileCommon.DisplayName + "</a>";
                else
                    litPageUpdateSection.Text += " by <a href=\"mailto:" + MyPage.CreatedBy.Email + "\">" + MyPage.CreatedBy.Name + "</a>";
            }
        }

        if (MyPage.LastUpdatedDate.Length > 0)
        {
            litPageUpdateSection.Text += "<br/>Last updated at " + MyPage.LastUpdatedDate;
            if (MyPage.LastUpdatedBy != null)
            {
                profileCommon = Profile.GetProfile(MyPage.LastUpdatedBy.Name);
                if (profileCommon.DisplayName.Length > 0)
                    litPageUpdateSection.Text += " by <a href=\"mailto:" + MyPage.LastUpdatedBy.Email + "\">" + profileCommon.DisplayName + "</a>";
                else
                    litPageUpdateSection.Text += " by <a href=\"mailto:" + MyPage.LastUpdatedBy.Email + "\">" + MyPage.LastUpdatedBy.Name + "</a>";
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            MyPage = null;
    }

    protected void lstLayout_Load(object sender, EventArgs e)
    {
        LoadMasterLayouts();
    }

    private void LoadMasterLayouts()
    {
        string[] masterLayouts = null;
        char[] pipeSplitter = { '|' };
        char[] commaSplitter = { ',' };

        masterLayouts = ConfigurationManager.AppSettings["AvailableMasterLayouts"].ToString().Split(pipeSplitter);
        foreach (string masterLayout in masterLayouts)
        {
            string[] layoutProperties = masterLayout.Split(commaSplitter);
            ((RadComboBox)fvEditPage.FindControl("lstLayout")).Items.Add(new RadComboBoxItem(layoutProperties[1].ToString(),
                                                                        layoutProperties[0].ToString()));
        }
    }
}
